package com.hxx.day10;

import org.junit.Test;

import java.io.*;
import java.util.Date;
import java.util.UUID;

public class TestDemo {
    /**
     * 测试字节输入流
     */
    @Test
    public void test1() {
        //字节输入流
        InputStream is = null;
        try {
            is = new FileInputStream("d:/zmx/one.txt");
            //一次读取一个字节
            int tmp = -1;
            while ((tmp=is.read())!=-1){
                //将字节转换为字符输出
                System.out.println((char)tmp);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 测试字节输入流-优化版
     * 流的关闭可以省略
     */
    @Test
    public void test2() {
        //字节输入流
        try(InputStream is = new FileInputStream("d:/zmx/one.txt")) {
            //一次读取一个字节数组
            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len=is.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 字节输出流
     */
    @Test
    public void test3() {
       try(InputStream in = new FileInputStream("d:/zmx/one.txt");
           OutputStream os = new FileOutputStream("d:/zmx/two.txt");){
           int tmp = -1;
           while ((tmp=in.read())!=-1){
               //将字节写入到输出流中
               os.write(tmp);
           }
           //刷新输出流
           os.flush();
       }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 字符输入输出流
     * 复制文本文件
     */
     @Test
     public void test4() throws Exception{
       //字符输入流
         Reader reader = new FileReader("d:/zmx/one.txt");
       //字符输出流
        Writer writer = new FileWriter("d:/zmx/three.txt");
        int tmp = 0;
        while ((tmp=reader.read())!=-1){
            //将字符写入到输出流中
            writer.write(tmp);
        }
        //刷新输出流
        writer.flush();
        //关闭字符输入流
        reader.close();
        //关闭字符输出流
        writer.close();
     }
    /**
     * 字节缓冲流
     */
     @Test
     public void test5() throws Exception{
        //字节输入流
        try(InputStream in = new FileInputStream("d:/zmx/one.txt");
            OutputStream os = new FileOutputStream("d:/zmx/four.txt");
            //字节缓冲输入流
            BufferedInputStream bis = new BufferedInputStream(in);
            //字节缓冲输出流
            BufferedOutputStream bos = new BufferedOutputStream(os);){
            int tmp = -1;
            while ((tmp=bis.read())!=-1){
                //将字节写入到输出流中
                bos.write(tmp);
            }
            //刷新输出流
            bos.flush();
        }
     }
    /**
     * 字符缓冲流
     */
     @Test
     public void test6() throws Exception{
        //字符输入流
        try(Reader reader = new FileReader("d:/zmx/one.txt");
            Writer writer = new FileWriter("d:/zmx/five.txt");
            //字符缓冲输入流
            BufferedReader br = new BufferedReader(reader);
            //字符缓冲输出流
            BufferedWriter bw = new BufferedWriter(writer);){
            String line = null;
            while ((line=br.readLine())!=null){
                //将字符写入到输出流中
                bw.write(line);
                //换行
                bw.newLine();
            }
            //刷新输出流
            bw.flush();
        }
     }
    /**
     * 字节字符转换流
     */
     @Test
     public void test7() throws Exception{
        //字节输入流
        try(InputStream in = new FileInputStream("d:/zmx/one.txt");
            OutputStream os = new FileOutputStream("d:/zmx/six.txt");
            //字节字符转换输入流
            InputStreamReader isr = new InputStreamReader(in);
            //字节字符转换输出流
            OutputStreamWriter osw = new OutputStreamWriter(os);
            //使用字符输入输出缓冲流
            //字符缓冲输入流
            BufferedReader br = new BufferedReader(isr);
            //字符缓冲输出流
            BufferedWriter bw = new BufferedWriter(osw);){
            String line =  null;
            while ((line = br.readLine())!=null){
                //将字符写入到输出流中
                bw.write(line);
                //换行
                bw.newLine();
            }
            //刷新输出流
            bw.flush();
        }
     }
    /**
     * 打印流：改变输出方向
     */
     @Test
     public void test8() throws Exception{
        //字节输出流
        try(OutputStream os = new FileOutputStream("d:/zmx/seven.txt");
            //打印流
            PrintStream ps = new PrintStream(os);){
            //将打印流设置为默认的输出流
            System.setOut(ps);
            System.out.println("hello world");
        }
     }
    /**
     * 对象流，完成序列化
     */
     @Test
     public void test9() throws Exception{
        //对象输出流
       try (ObjectOutputStream oos =
                    new ObjectOutputStream(new FileOutputStream("d:/zmx/eight.txt"));){
           // 创建一个Emp对象
           Emp emp = new Emp(1001,"张三",20,"123456");
            //将emp对象写入到输出流中
            oos.writeObject(emp);
            oos.flush();
       }catch (Exception e) {
            e.printStackTrace();
        }
     }
    /**
     * 对象流，完成反序列化
     */
     @Test
     public void test10() throws Exception{
        //对象输入流
       try (ObjectInputStream ois =
                    new ObjectInputStream(new FileInputStream("d:/zmx/eight.txt"));){
           //从输入流中读取emp对象
           Emp emp = (Emp) ois.readObject();
           System.out.println(emp);
       }catch (Exception e) {
            e.printStackTrace();
        }
     }
    /**
     * File文件
     */
     @Test
     public void test11() throws Exception{
        //创建一个File对象
        File file = new File("d:/zmx/eight.txt");
        if (file.isDirectory()){
            //获取当前目录下的所有文件
            File[] files = file.listFiles();
        }

         //判断文件是否存在
        System.out.println(file.exists());
        //判断是否是一个文件
        System.out.println(file.isFile());
        //判断是否是一个目录
        System.out.println(file.isDirectory());
        //获取文件的名称
        System.out.println(file.getName());
        //获取文件的路径
        System.out.println(file.getPath());
        //获取文件的绝对路径
        System.out.println(file.getAbsolutePath());
        //获取文件的父目录
        System.out.println(file.getParent());
        //获取文件的大小
        System.out.println(file.length());
        //获取文件的最后修改时间
        System.out.println(new Date(file.lastModified()));
     }
    /**
     * 文件上传
     */
     @Test
     public void test12() {
         try{
             File file = new File("D:\\课程资料\\springboot3+swagger3.docx");
             //读取文件流
             InputStream in = new FileInputStream(file);
             //获取文件的名称
             String fileName = file.getName();
             //获取文件的扩展名
             String fileExtension = fileName.substring(fileName.lastIndexOf("."));
             //新的文件名称
             String fn = UUID.randomUUID().toString();
             File file1 = new File("d:/zmx",fn+fileExtension);
             //添加名称和路径
             OutputStream os = new FileOutputStream(file1);
             //循环的方式写入文件
             int len = 0;
             byte[] bytes = new byte[1024];
             while ((len=in.read(bytes))!=-1){
                 os.write(bytes,0,len);
             }
             //刷新输出流
             os.flush();
             //关闭输出流
             os.close();
             //关闭输入流
             in.close();
         }catch (Exception e){
             e.printStackTrace();
         }
     }
}

class Emp implements Serializable{
    //序列化版本号,为了兼容不同版本的类
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private Integer age;
    //transient 关键字,表示该字段不会被序列化
    private transient String password;

    public Emp() {
    }

    public Emp(Integer id, String name, Integer age, String password) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", password='" + password + '\'' +
                '}';
    }
}

























